package com.sunny.controller;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sunny.entity.User;
import com.sunny.services.UserService;

/**
 * 
 * @author Catkin_nice
 *
 */
@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private UserService userService;
	
	@PostMapping
	public String addUser(User user) {
		userService.addUser(user);
		return "redirect:/user/list";
	}
	
	@GetMapping("/{userid}")
	public String getUser(@PathVariable Long userid, Model model) {
		User user = userService.getUser(userid);
		model.addAttribute("user", user);
		return "/user/user_info";
	}
	
	@GetMapping("/list")
	public String getUsers(Model model) {
		Collection<User> users = userService.getUsers();
		model.addAttribute("users", users);
		return "/user/show_users";
	}
	
}
